package com.sftz.modules.minapp.teachActivity.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.google.common.collect.Maps;
import com.sftz.assets.entities.InsCourseStructureEntity;
import com.sftz.assets.entities.teachactivity.TeachActivityEntity;
import com.sftz.assets.entities.teachactivity.TeachActivityPostponeEntity;
import com.sftz.assets.enums.FileSizeEnum;
import com.sftz.framework.util.CheckTimeUtil;
import com.sftz.modules.minapp.teachActivity.dto.TeachActivityStudyDto;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

/**
 * @author   DuKL  教学活动
 * @Date	 2018年12月19日
 */
@IocBean
public class TeachActivityStudyViewService {
	@Inject
	private IDbDao dbDao;

	@Inject
	private SqlManager sqlManager;

	/**
	 * @author   DuKL
	 * @param courseStruId 
	 * @Date	 2018年12月19日
	 * (effect:查询当前课程下的所有教学实践)
	 */
	public Map<String, Object> courseTeachActivityInfo(Integer courseId, Integer userId) {
		Map<String, Object> obj = Maps.newHashMap();
		if (!Util.isEmpty(courseId)) {
			Sql teachSql = Sqls.create(sqlManager.get("minapp_teachActivity_course_all_teach_list"));
			teachSql.setParam("courseId", courseId);
			List<TeachActivityStudyDto> courseTeach = DbSqlUtil.query(dbDao, TeachActivityStudyDto.class, teachSql);
			//查询当前教学实践的状态
			for (TeachActivityStudyDto teachActivity : courseTeach) {
				//判断这个教学实践的结束时间(是否被延期)
				TeachActivityPostponeEntity postpone = dbDao.fetch(TeachActivityPostponeEntity.class,
						Cnd.where("activityId", "=", teachActivity.getTeachId()).and("userId", "=", userId));
				if (!Util.isEmpty(postpone)) {//有需要延迟的时间记录
					Date postDate = postpone.getPostDate();
					teachActivity.setEndDate(postDate);
				}
				//设置活动的状态
				Date date = new Date();
				if (!Util.isEmpty(teachActivity.getStartDate()) && !Util.isEmpty(teachActivity.getEndDate())) {
					int flag = CheckTimeUtil.isStart(date, teachActivity.getStartDate(), teachActivity.getEndDate());
					teachActivity.setFlag(flag);
				}
			}
			obj.put("courseTeach", courseTeach);
		}
		return obj;
	}

	public Object activitSingleMessage(Integer userId, Integer courseStruId) {
		Map<String, Object> obj = new HashMap<String, Object>();
		//查询当前教学实践的信息
		Sql sql = Sqls.create(sqlManager.get("minapp_teachActivity_activitSingleMessage"));
		sql.setCondition(Cnd.where("courseStruId", "=", courseStruId));
		TeachActivityEntity activityEntity = DbSqlUtil.fetchEntity(dbDao, TeachActivityEntity.class, sql);
		TeachActivityStudyDto teachActivityStudyDto = new TeachActivityStudyDto();
		if (!Util.isEmpty(activityEntity)) {
			InsCourseStructureEntity fetch = dbDao.fetch(InsCourseStructureEntity.class,
					Cnd.where("id", "=", courseStruId));
			teachActivityStudyDto.setTeachName(fetch.getName());
			String fileSize = activityEntity.getFileSize();
			if (!Util.isEmpty(fileSize)) {
				FileSizeEnum[] values = FileSizeEnum.values();
				for (FileSizeEnum fileSizeEnum : values) {
					if (fileSizeEnum.key().equals(activityEntity.getFileSize())) {
						teachActivityStudyDto.setFileSize(fileSizeEnum.value());
					}
				}
			}
			Integer fileNum = activityEntity.getFileNum();
			if (!Util.isEmpty(fileNum)) {
				teachActivityStudyDto.setFileNum(fileNum);
			}
			Double score = activityEntity.getScore();
			if (!Util.isEmpty(score)) {
				teachActivityStudyDto.setScore(score);
			}
			Integer courseId = activityEntity.getCourseId();
			if (!Util.isEmpty(courseId)) {
				teachActivityStudyDto.setCourseId(courseId);
			}
			//判断这个教学实践的结束时间(是否被延期)
			TeachActivityPostponeEntity postpone = dbDao.fetch(TeachActivityPostponeEntity.class,
					Cnd.where("activityId", "=", activityEntity.getId()).and("userId", "=", userId));
			if (!Util.isEmpty(postpone)) {//有需要延迟的时间记录
				Date postDate = postpone.getPostDate();
				activityEntity.setEndDate(postDate);
			}
			//设置活动的状态
			Date date = new Date();
			if (!Util.isEmpty(activityEntity.getStartDate()) && !Util.isEmpty(activityEntity.getEndDate())) {
				int flag = CheckTimeUtil.isStart(date, activityEntity.getStartDate(), activityEntity.getEndDate());
				teachActivityStudyDto.setFlag(flag);
				teachActivityStudyDto.setStartDate(activityEntity.getStartDate());
				teachActivityStudyDto.setEndDate(activityEntity.getEndDate());
			}
			obj.put("teachAc", teachActivityStudyDto);
		}
		return obj;
	}
}
